package JZOffer;

// 二维数组中的查找
public class JzOffer04 {
    public boolean findNumberIn2DArray(int[][] matrix, int target) {
        for (int[] x : matrix) {
            int res = search(x, target);
            if (res > 0) {
                return true;
            }
        }
        return false;
    }

    private int search(int[] x, int target) {
        int l = 0;
        int r = x.length-1;

        while (l <= r) {
            int mid = l + (r-l)/2;
            if (x[mid] < target) {
                l = mid + 1;
            } else if (x[mid] > target) {
                r = mid - 1;
            } else {
                return x[mid];
            }
        }
        return -1;
    }
}
